<?
	class fileupload
	{
		var $pathStorage;
		var $sizeLimit;
		var $extensions;
		var $fileIn;
		var $filename;
		var $message;
		var $result;
		var $tof;

		function fileupload($size,$extension,$file) {
			$this->sizeLimit 	= $size;
			$this->extensions 	= $extension;
			$this->fileIn 		= $file;
			$this->message		= array("Attention ... no file selected!",
							"Your file has been sucessfully uploaded!",
							"Attention ... is not possible to copy the file!",
							"Attention ... file size too much big!",
							"Attention ... file extension not supported!",
							"Attention ... file path not exists!");
		}

		function verifyExtension($extensions,$fileExt) {
				$this->tof = false;
				foreach($extensions as $k => $v) {
						if ($v == $fileExt) {
							$this->tof = true;
						}						
				}
				return $this->tof;
		}

		function verifySize($size,$fileSize) {
				$this->tof = false;
				if ($fileSize <= $size) {
					$this->tof = true;
				}
				return $this->tof;
		}

		function makedirectory() {
			if (file_exists($this->pathStorage)) {
			    return true;
			} else {
				$chk = mkdir($this->pathStorage,0755);

				if ($chk)
					return true;
				else
			    	return false;
			}
		}

		function get_file_extension() {
			switch ($this->fileIn['type']) {
			case "image/pjpeg" :
				$filetype = ".jpg";
				break;
			case "image/jpg" :
				$filetype = ".jpg";
				break;
			case "image/jpeg" :
				$filetype = ".jpg";
				break;
			case "image/gif" :
				$filetype = ".gif";
				break;
			case "image/x-png":
				$filetype = ".png";
				break;
			case "application/x-shockwave-flash" :
				$filetype = ".swf";
				break;
			}
			return $filetype;
		}

		function upload($path,$filename)
		{
			$this->pathStorage 	= $path;
			$this->filename 	= $filename;
			$filetype			= $this->get_file_extension();

			if ($this->fileIn['name'] == "")
			{
				$this->result = 0;
			}
			else
			{
				// Verify extensions admitted
				if ($this->verifyExtension($this->extensions,$this->fileIn['type']))
				//if ($this->verifyExtension($this->extensions,$filetype))
				{
					// Verify file size
					if ($this->verifySize($this->sizeLimit,$this->fileIn['size']))
					{
						// file extension and size are OK
						if ($this->makedirectory())
						{
							//directory exists
							if (@copy($this->fileIn['tmp_name'],$this->pathStorage.$this->filename.$filetype))
							{
								$this->result = 1;
							}
							else
							{
								$this->result = 2;
							}
						}
						else
						{
							$this->result = 5;
						}
					}
					else
					{
						$this->result = 3;
					}
				}
				else
				{
					$this->result = 4;
				}
			}
			return $this->result;

		}//function

	}//class

?>